<?php
/**
 * Created by PhpStorm.
 * User: 煌煌
 * Date: 2020/12/10
 * Time: 14:10
 * Email:zenghuang19@163.com
 */

namespace App\Services;


use App\Models\Article;
use App\Models\Product;
use App\Models\Video;
use Illuminate\Support\Facades\Redis;

class IndexService
{
    public static function indexCache()
    {
        $data = [];

        $url = env('QINIUURL');

        //首页轮播图
        $data['slideShow'] = Product::with('product_auxiliaries')->where([
            'is_release' => 1,
            'is_home'    => 1
        ])->get(['url', 'id']);

        $data['slideShow'] = $data['slideShow']->map(function ($value, $key) use ($url) {
            $value['url']         = $url . '/' . $value['url'];
            $value['height']      = $value['product_auxiliaries']['height'];
            $value['bust']        = $value['product_auxiliaries']['bust'];
            $value['waist']       = $value['product_auxiliaries']['waist'];
            $value['hips']        = $value['product_auxiliaries']['hips'];
            $value['shoe']        = $value['product_auxiliaries']['shoe'];
            $value['hair_colour'] = $value['product_auxiliaries']['hair_colour'];
            $value['eyes']        = $value['product_auxiliaries']['eyes'];

            return $value;
        });

        $cache = Redis::get('Info');
        $cache = json_decode($cache, true);
        //公司简介
        $data['companyProfile']   = $cache['company_profile']; //公司简介
        $data['companyTitle']     = $cache['title']; //公司名称
        $data['abbreviation']     = $cache['abbreviation']; //公司简称
        $data['companyEngTitle']  = $cache['company']; //公司名称--英文
        $data['companyImage']     = $url . '/' . $cache['company_image']; //公司宣传图片
        $data['companyAdvantage'] = explode('+', $cache['company_advantage']); //公司优势
        $data['allModelTitle']    = $cache['all_model_title'];
        $data['allModelDesc']     = $cache['all_model_desc'];
        //视频
        $data['videos'] = Video::where([
            'is_release' => 1
        ])->orderBy('id', 'desc')->first([
            'url',
            'image'
        ]);

        if (isset($data['videos']->url)) {
            $data['videos']->url   = $url . '/' . $data['videos']->url;
            $data['videos']->image = $url . '/' . $data['videos']->image;
        }


        //首页推荐模特
        $data['product'] = Product::query()
                                  ->with('product_auxiliaries')
                                  ->where('is_suggest', 1)
                                  ->offset(0)
                                  ->limit(9)
                                  ->get([
                                      'id',
                                      'image',
                                      'title'
                                  ]);

        if (!($data['product']->isEmpty())) {
            $data['product'] = $data['product']->map(function ($value, $key) use ($url) {
                $value['image']       = $url . '/' . $value['image'];
                $value['height']      = $value['product_auxiliaries']['height'];
                $value['bust']        = $value['product_auxiliaries']['bust'];
                $value['waist']       = $value['product_auxiliaries']['waist'];
                $value['hips']        = $value['product_auxiliaries']['hips'];
                $value['shoe']        = $value['product_auxiliaries']['shoe'];
                $value['hair_colour'] = $value['product_auxiliaries']['hair_colour'];
                $value['eyes']        = $value['product_auxiliaries']['eyes'];

                return $value;
            });
        }

        $data['article'] = Article::query()->where([
            'is_star'    => Article::RECOMMEND,
            'is_release' => Article::RELEASE
        ])->offset(0)->limit(3)->get(['title', 'image', 'real_pv', 'pv', 'created_at', 'id']);
        if (!($data['article']->isEmpty())) {
            $data['articleYes'] = 1;
            $data['article']    = $data['article']->map(function ($value, $key) use ($url) {
                $value['image']  = $url . '/' . $value['image'];
                $value['number'] = $value['real_pv'] + $value['pv'];

                return $value;
            });
        } else {
            $data['articleYes'] = 0;
        }


        $seo = Redis::get('seo');

        $data['seo'] = json_decode($seo, true);

        $res = json_encode($data);

        //首页缓存
        Redis::setex('index_content', 36000, $res);

        return $data;
    }
}
